// pages/cart/cart.js
import {getCartList, deleteCartPro} from "../../api/cart"
let startX = 0;
let endX = 0;
let offsetX = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    total: 0,
    price: 0,
    cartList: [],
    isSelectAll: false,
    animationData: null,
    animationData1: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    const data = await getCartList() 
    console.log(data);
    data.data.data.forEach((item)=> {
      item.isSelect = false
    })

    var animation = wx.createAnimation({
      duration: 600,
      timingFunction: 'ease',
    })

    animation.translateX(0).step()
    this.setData({
      cartList: data.data.data,
      animationData1:animation.export()
    })
    
  },
  // 切换选中或者是未选中状态
  changeStatus(event) {
    let index = event.target.dataset.index
    let list = this.data.cartList
    list[index].isSelect = !list[index].isSelect

    const flag = list.every((item)=> {
      return item.isSelect === true
    })

    this.setData({
      cartList: list,
      isSelectAll: flag
    }, ()=> {
      this.caclTotal()
    })

  },
  caclTotal() {
    const total = this.data.cartList.reduce((total,item)=> {
      if(item.isSelect) {
        total += item.number
      }

      return total
    }, 0)

    // 计算价格
    const price = this.data.cartList.reduce((total,item)=> {
      if(item.isSelect) {
        total += item.number*item.retail_price
      }

      return total
    }, 0)

    this.setData({
      total,
      price
    })
  },
  selectAll() {
    const flag = !this.data.isSelectAll
    let list = this.data.cartList
    list.forEach((item)=> {
      item.isSelect = flag
    })
 
    this.setData({
      isSelectAll: flag,
      cartList: list
    },()=> {
      this.caclTotal()
    })
  },
  // 滑动开始
  touchStart(event) {
    console.log(event.touches[0].pageX)
    startX = event.touches[0].pageX
    this.setData({
      currentIndex: event.target.dataset.index
    })
  },
  // 滑动中
  touchMove(event) {
    console.log(event.touches[0].pageX)
    endX = event.touches[0].pageX
  },
  // 滑动结束
  touchEnd(event) {
    offsetX = startX - endX;
    if(offsetX > 0 && offsetX > 30) {
      // 向左滑
      var animation = wx.createAnimation({
        duration: 600,
        timingFunction: 'ease',
      })

      animation.translateX(-50).step()

      this.setData({
        animationData:animation.export()
      })
    }else {
      // this.setData({
      //   currentIndex: ""
      // })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  deleteCart(event) {
    wx.showModal({
      title:"是否要删除改商品",
      cancelColor: 'cancelColor',
      success: async (res)=> {
        console.log(res)
        const index = event.target.dataset.index
        console.log("=====",index)
        if(res.confirm) {
          // 确认要删除
          // 删除购物车商品
          const data = await deleteCartPro(this.data.cartList[event.target.dataset.index].id)

          let list = this.data.cartList
          list.splice(event.target.dataset.index, 1)
          this.setData({
            cartList: list
          })

        }else {

        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})